# !/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:zzly
@file:views.py
@time:2020/09/08
"""
from rest_framework.views import APIView
from rest_framework import generics
from utils import response_ok
from apps.adorn_pedia.models import AdornPedia, AdornPediaImgs
from apps.system.models import Option
from apps.system.serializers import OptionDictSerializer
from .serializers import AdornPediaDetailAppSeializer, AdornPediaCategorySerializer, AdornPediaImgsSerializers
from apps.adorn_pedia.filters import AdornPediaImgsFilter


class AdornPediaDictView(APIView):
    def get(self, request):
        ret = {
            'category': OptionDictSerializer(Option.objects.filter(type=Option.ADORN_PEDIA_CATEGORY, enable=True),
                                             many=True).data
        }
        return response_ok(ret)


class AdornPediaCategoryListView(generics.ListAPIView):
    queryset = Option.objects.filter(type=Option.ADORN_PEDIA_CATEGORY, enable=True)
    serializer_class = AdornPediaCategorySerializer

    def get(self, request, *args, **kwargs):
        serializer = self.serializer_class(self.get_queryset(), many=True)
        return response_ok(serializer.data)

    def paginate_queryset(self, queryset):
        return None


class AdornPediaDetailView(generics.RetrieveAPIView):
    queryset = AdornPedia.objects.filter()
    serializer_class = AdornPediaDetailAppSeializer


class AdornPediaImgsView(generics.ListAPIView):
    queryset = AdornPediaImgs.objects.filter()
    serializer_class = AdornPediaImgsSerializers

    def filter_queryset(self, queryset):
        f = AdornPediaImgsFilter(self.request.GET, queryset=self.queryset)
        return f.qs
